<?php

/**
 * @author Recep UNCU
 * @copyright 2012
 * www.ikradata.com
 */

class database {
	
	private $link = NULL;
	
	function database($username, $password, $databasename, $hostname){
		$this->link = @new mysqli($hostname, $username, $password, $databasename) or die("Connection Error!");
	} #Database End;

        public function Destroy(){
			$this->link->close();
        } #Destroy End;
	
        private function execute($SQL) {
			return $this->link->query($SQL, MYSQLI_USE_RESULT);
        } #ExecuteSQL End;
		
	    private function escape($string) {
	        return htmlspecialchars($string, ENT_QUOTES);
	    } #escape End;
		
		public function QuotedStr($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""){
		  if (PHP_VERSION < 6): $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; endif;
		  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($this->link, $theValue) : mysqli_real_escape_string($this->link, $theValue);
		  switch ($theType):
		    case "text"	  : $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";			break;
		    case "long"	  : case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break;
		    case "double" : $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";			break;
		    case "date"	  : $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";			break;
		    case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;	break;
		  endswitch; return $theValue;
		} #QuotedStr End;

        public function DeleteSQL($TableName, $clause){       	
        	$SQL = "DELETE FROM {$TableName} {$clause}";
            $AfterDelete = $this->execute($SQL);
            if($AfterDelete): return true; else: return false; endif;
        } #DeleteSQL End;

        public function InsertSQL($TableName, $fields=array(), $DataField=array(), $InsertMethod="INSERT"){
        	echo "<br>debut de la fct";
        	$sets = $InsertMethod;
			if($InsertMethod=="REPLACE"):$sets="REPLACE";endif;
            $columns = "(`".implode('`,`', $fields)."`)"; 
            $values = "('".implode("','", array_map(array($this, 'escape'), $DataField))."')";
        	$SQL = "{$sets} INTO {$TableName} {$columns} VALUES {$values};";
        	echo "{$sets} INTO {$TableName} {$columns} VALUES {$values};";
            $AfterPost = $this->execute($SQL);
            if($AfterPost): return ($this->link->insert_id != "") ? $this->link->insert_id : true; endif;
        } #InsertSQL End;

        public function ModifySQL($TableName, $clause, $fields = array(), $DataField = array()){
        	$sets = array(); $brackets = null;

        	$DataSource = array_combine($fields, $DataField);
        	foreach ($DataSource as $field => $data){
        	$sets[].= "{$field} = '{$data}'";
        	} $brackets = implode(",", $sets);
			$SQL = "UPDATE {$TableName} SET {$brackets} {$clause};";
			        	
            $AfterEdit = $this->execute($SQL);
            if($AfterEdit): return true; else: return false; endif;
        } #ModifySQL End;

        public function Query($SQL){
			$data = array();

			$DataSource = $this->execute($SQL);

			while ($row = $DataSource->fetch_assoc()) : $data[] = $row; endwhile; 
				$DataSource->close();
				$this->link->next_result();

			return $data;
        } #Query End;
	
        public function Table($TableName, $clause=NULL){
			$data = array(); 			
			$SQL = "SELECT * FROM {$TableName} {$clause};"; 			
			$DataSource = $this->execute($SQL);
			
			while ($row = $DataSource->fetch_assoc()) : $data[] = $row; endwhile; 
			$DataSource->close();
			$this->link->next_result();
			
			return $data;
        } #Table End;

		public function DBText($SQL){
            $DataSource = $this->execute($SQL);
            if($DataSource):
                return $DataSource->fetch_object();
			endif; mysqli_free_result($DataSource);
		} #DBText End;		

        public function RecordCount(){
		$RecordCount = $this->DBText("SELECT FOUND_ROWS() AS `found_rows`;");
		return $RecordCount->found_rows;
        } #RecordCount End;
	   public function loadIdTacheRealise($date) {
         $requete_prepare_2=connexion()->prepare("SELECT idTacheRealisee FROM tacheRealisee WHERE dateFin ='$date' "); // on prépare notre requête
         $requete_prepare_2->execute();
         while($lignes=$requete_prepare_2->fetch(PDO::FETCH_OBJ)) {
            $id=$lignes->idTacheRealisee;
         }
     return $id; //la fonction retourne l'id de la tache realise
}
} #class database End.

?>